package impl

import (
	"adam2/internal/constants"
	"adam2/internal/vo"
	pConstants "anubis-framework/pkg/constants"
	pDomain "anubis-framework/pkg/domain"
	"anubis-framework/pkg/util"
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
)

type MonitorCurrentPriceControllerImpl struct {
	*BaseControllerImpl
}

// 获取MonitorCurrentPriceControllerImpl
func GetMonitorCurrentPriceControllerImpl() *MonitorCurrentPriceControllerImpl {
	return &MonitorCurrentPriceControllerImpl{GetBaseControllerImpl()}
}

// 分页显示
func (_monitorCurrentPriceControllerImpl *MonitorCurrentPriceControllerImpl) Page(c *gin.Context) {
	// 转换参数
	var pageMonitorCurrentPriceVo *vo.PageMonitorCurrentPriceVo = &vo.PageMonitorCurrentPriceVo{}
	c.BindJSON(pageMonitorCurrentPriceVo)

	// 参数验证：参数pageNo和pageSize不能为空
	if pageMonitorCurrentPriceVo.PageNo == 0 || pageMonitorCurrentPriceVo.PageSize == 0 {
		var apiResult *pDomain.ApiResult = &pDomain.ApiResult{}
		apiResult.Code = pConstants.OPERATION_FAIL_CODE
		apiResult.Message = "参数pageNo和pageSize不能为空"
		apiResult.Success = false

		c.JSON(http.StatusBadRequest, *apiResult)
		return
	}

	// 分页
	var serviceResult *pDomain.ServiceResult = _monitorCurrentPriceControllerImpl._monitorCurrentPriceServiceImpl.Page(pageMonitorCurrentPriceVo.TransactionType, pageMonitorCurrentPriceVo.Code, pageMonitorCurrentPriceVo.Name, pageMonitorCurrentPriceVo.Available, pageMonitorCurrentPriceVo.PageNo, pageMonitorCurrentPriceVo.PageSize)
	var code, apiResult = util.ServiceResultToApiResult(serviceResult)
	c.JSON(code, *apiResult)
	return
}

// 添加
func (_monitorCurrentPriceControllerImpl *MonitorCurrentPriceControllerImpl) Add(c *gin.Context) {
	// 转换参数
	var addMonitorCurrentPriceVo *vo.AddMonitorCurrentPriceVo = &vo.AddMonitorCurrentPriceVo{}
	c.BindJSON(addMonitorCurrentPriceVo)

	// 参数验证：参数transactionType、code、name、comparison、monitorPrice、available不能为空
	if addMonitorCurrentPriceVo.TransactionType == 0 || addMonitorCurrentPriceVo.Code == "" ||
		addMonitorCurrentPriceVo.Name == "" || addMonitorCurrentPriceVo.Comparison == 0 ||
		addMonitorCurrentPriceVo.MonitorPrice == 0 || addMonitorCurrentPriceVo.Available == 0 {
		var apiResult *pDomain.ApiResult = &pDomain.ApiResult{}
		apiResult.Code = pConstants.OPERATION_FAIL_CODE
		apiResult.Message = "参数name不能为空"
		apiResult.Success = false

		c.JSON(http.StatusBadRequest, *apiResult)
		return
	}
	// 参数验证：根据transactionType、code、name、comparison、monitorPrice、available判断记录是否已经存在
	var serviceResult *pDomain.ServiceResult = _monitorCurrentPriceControllerImpl._monitorCurrentPriceServiceImpl.
		ExistByTransactionTypeAndCodeAndNameAndComparisonAndMonitorPriceAndAvailable(addMonitorCurrentPriceVo.TransactionType, addMonitorCurrentPriceVo.Code,
			addMonitorCurrentPriceVo.Name, addMonitorCurrentPriceVo.Comparison, addMonitorCurrentPriceVo.MonitorPrice, addMonitorCurrentPriceVo.Available)
	if serviceResult.Success == false {
		var apiResult *pDomain.ApiResult = &pDomain.ApiResult{}
		apiResult.Code = pConstants.OPERATION_FAIL_CODE
		apiResult.Message = "参数transactionType、code、name、comparison、monitorPrice、available不能重复"
		apiResult.Success = false

		c.JSON(http.StatusBadRequest, *apiResult)
		return
	}

	// 插入记录
	var _serviceResult *pDomain.ServiceResult = _monitorCurrentPriceControllerImpl._monitorCurrentPriceServiceImpl.Add(addMonitorCurrentPriceVo)
	var code, apiResult = util.ServiceResultToApiResult(_serviceResult)
	c.JSON(code, *apiResult)
	return
}

// 结束监控
func (_monitorCurrentPriceControllerImpl *MonitorCurrentPriceControllerImpl) StopMonitorCurrentPrice(c *gin.Context) {
	var id, _ = strconv.Atoi(c.Param("id"))

	// 验证参数
	if id == 0 {
		var apiResult *pDomain.ApiResult = &pDomain.ApiResult{}
		apiResult.Code = pConstants.OPERATION_FAIL_CODE
		apiResult.Message = "参数id不能为空"
		apiResult.Success = false

		c.JSON(http.StatusBadRequest, *apiResult)
		return
	}

	// 根据id更新available
	var serviceResult *pDomain.ServiceResult = _monitorCurrentPriceControllerImpl._monitorCurrentPriceServiceImpl.UpdateAvailableById(id, constants.NOT_AVAILABLE)
	var code, apiResult = util.ServiceResultToApiResult(serviceResult)
	c.JSON(code, *apiResult)
	return
}

// 删除监控
func (_monitorCurrentPriceControllerImpl *MonitorCurrentPriceControllerImpl) DeleteMonitorCurrentPrice(c *gin.Context) {
	var id, _ = strconv.Atoi(c.Param("id"))

	// 验证参数
	if id == 0 {
		var apiResult *pDomain.ApiResult = &pDomain.ApiResult{}
		apiResult.Code = pConstants.OPERATION_FAIL_CODE
		apiResult.Message = "参数id不能为空"
		apiResult.Success = false

		c.JSON(http.StatusBadRequest, *apiResult)
		return
	}

	// 根据id删除监控
	var serviceResult *pDomain.ServiceResult = _monitorCurrentPriceControllerImpl._monitorCurrentPriceServiceImpl.DeleteMonitorCurrentPrice(id)
	var code, apiResult = util.ServiceResultToApiResult(serviceResult)
	c.JSON(code, *apiResult)
	return
}
